//
//  LLGStatuseToolBar.swift
//  Weibo30LLG
//
//  Created by 李龙 on 16/8/12.
//  Copyright © 2016年 lilong. All rights reserved.
//

import UIKit
import SnapKit

class LLGStatuseToolBar: UIView {
    
    var statuseViewModel: LLGStatuseViewModel? {
    
        //设置空间信息
        didSet {
        
            retweetButton?.setTitle(statuseViewModel?.retweetCountContent, forState: .Normal)
            commentButton?.setTitle(statuseViewModel?.commentCountContent, forState: .Normal)
            unlikeButton?.setTitle(statuseViewModel?.unlikeCountContent, forState: .Normal)
        }
    }
    
    //转发
    private var retweetButton: UIButton?
    //评论
    private var commentButton: UIButton?
    //点赞
    private var unlikeButton: UIButton?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: --布局子控件
    private func setupUI() {
    
        //添加控件
        retweetButton = addChildButton("timeline_icon_retweet", title: "转发")
        commentButton = addChildButton("timeline_icon_comment", title: "评论")
        unlikeButton = addChildButton("timeline_icon_unlike", title: "赞")
        
        let fristLineView: UIImageView = addLineView()
        let secondLineView: UIImageView = addLineView()
        
        //添加约束
        retweetButton!.snp_makeConstraints { (make) -> Void in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(commentButton!)
        }
        commentButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(retweetButton!)
            make.leading.equalTo(retweetButton!.snp_trailing)
            make.width.equalTo(unlikeButton!)
        }
        unlikeButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(commentButton!)
            make.trailing.equalTo(self)
            make.leading.equalTo(commentButton!.snp_trailing)
        }
        
        fristLineView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self)
            make.centerX.equalTo(retweetButton!.snp_trailing)
        }
        secondLineView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self)
            make.centerX.equalTo(commentButton!.snp_trailing)
        }
        
    }
    
    // MARK: --添加按钮
    private func addChildButton(imageName: String, title: String) -> UIButton {
    
        let button: UIButton = UIButton()
        
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Normal)
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setTitle(title, forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        addSubview(button)
        
        return button
    }

    // MARK: --添加线
    private func addLineView() -> UIImageView {
    
        let lineView: UIImageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        
        addSubview(lineView)
        
        return lineView
    }
}
